package top.whysu.edevice.service.relative.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.whysu.edevice.constant.CodeConstant;
import top.whysu.edevice.dao.relative.RelativeSecretKeyTokenDao;
import top.whysu.edevice.exception.BusinessException;
import top.whysu.edevice.po.relative.RelativeSecretKeyTokenPO;
import top.whysu.edevice.service.relative.RelativeSecretKeyTokenService;
import top.whysu.edevice.utils.DateUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Service("RelativeSecretKeyTokenServiceImpl")
public class RelativeSecretKeyTokenServiceImpl implements RelativeSecretKeyTokenService {
    private static final Logger LOGGER = LoggerFactory.getLogger(RelativeSecretKeyTokenServiceImpl.class);

    @Resource
    private RelativeSecretKeyTokenDao relativeSecretKeyTokenDao;

    @Override
    public synchronized void wholeSecretKeyToken(BigDecimal secretKeyId, String accessToken, Long expireTime) {
        if (secretKeyId == null || StringUtils.isBlank(accessToken) || expireTime == null) {
            throw new BusinessException(CodeConstant.Http.EXECUTE_FAIL, "AccessToken相关参数不能为空！");
        }
        RelativeSecretKeyTokenPO po = new RelativeSecretKeyTokenPO();
        po.setAccessToken(accessToken);
        po.setExpireTime(expireTime);
        // 查询数据库
        RelativeSecretKeyTokenPO dbPO = relativeSecretKeyTokenDao.queryBySecretKeyId(secretKeyId);
        if (dbPO != null) {
            po.setId(dbPO.getId());
            relativeSecretKeyTokenDao.update(po);
        } else {
            po.setSecretKeyId(secretKeyId);
            relativeSecretKeyTokenDao.insert(po);
        }
    }
}
